﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using QuanLyBus.Model;
using testSMS;

namespace QuanLyBus
{
    public partial class Them_tuyen : PhoneApplicationPage
    {
        QL_TuyenDataContext db = new QL_TuyenDataContext(App.constr);
        classGoTV gotvTxtBody = new classGoTV();//tao đối tượng
        ds_tuyen tuyen = new ds_tuyen();
        public Them_tuyen()
        {
            InitializeComponent();
            tuyen = db.ds_tuyens.Single(p=>p.id_tuyen==App.id);
            txt_id_tuyen.Text = tuyen.id_tuyen.ToString();
            txt_ten_tuyen_den.Text = tuyen.ten_tuyen_den;
            txt_ten_tuyen_di.Text = tuyen.ten_tuyen_di;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.NavigationService.GoBack();
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            this.NavigationService.GoBack();
            base.OnBackKeyPress(e);
        }        
        private void btn_save_Click(object sender, EventArgs e)
        {
            ds_tuyen item = new ds_tuyen();
            item = db.ds_tuyens.Single(p => p.id_tuyen == App.id);
            item.ten_tuyen_den = txt_ten_tuyen_den.Text;
            item.ten_tuyen_di = txt_ten_tuyen_di.Text;
            db.SubmitChanges();
            MessageBox.Show("Sửa thành công");
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            txt_ten_tuyen_den.AddHandler(TextBox.KeyDownEvent, new KeyEventHandler(txt_ten_tuyen_den_KeyDown), true);
            txt_ten_tuyen_di.AddHandler(TextBox.KeyDownEvent, new KeyEventHandler(txt_ten_tuyen_di_KeyDown), true);

        }

        private void txt_ten_tuyen_di_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox t = sender as TextBox;

            if (e.Key == Key.Back)

                gotvTxtBody.XoaKtu = true;

            else
            {

                if (e.Key == Key.Space)
                {

                    gotvTxtBody.NhapSpace = true;
                    gotvTxtBody.CoNguyenAm = false;

                }

            }
        }

        private void txt_ten_tuyen_di_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (gotvTxtBody.TextChanged == 2)
            {

                gotvTxtBody.TextChanged = 0;

                gotvTxtBody.GoDauTV(ref txt_ten_tuyen_di);

            }

            gotvTxtBody.TextChanged++;
        }

        private void txt_ten_tuyen_den_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (gotvTxtBody.TextChanged == 2)
            {

                gotvTxtBody.TextChanged = 0;

                gotvTxtBody.GoDauTV(ref txt_ten_tuyen_den);

            }

            gotvTxtBody.TextChanged++;
        }

        private void txt_ten_tuyen_den_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox t = sender as TextBox;

            if (e.Key == Key.Back)

                gotvTxtBody.XoaKtu = true;

            else
            {

                if (e.Key == Key.Space)
                {

                    gotvTxtBody.NhapSpace = true;
                    gotvTxtBody.CoNguyenAm = false;

                }

            }
        }
        
    }
}